package org.dcarew.model.core;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.dcarew.model.core.impl.ModelClass;

/**
 * 
 * 
 * @author Devon Carew
 */
public abstract class ModelObject
	implements IPropertySource
{
	private Map<String, Object> values = new HashMap<String, Object>();
	
	private Set<IPropertyListener> listeners = new HashSet<IPropertyListener>();
	
	private IModelClass modelClass;
	
	
	public ModelObject()
	{
		
	}
	
	public Object getPropertyValue(String property)
	{
		return values.get(property);
	}
	
	public Object getPropertyValue(IModelProperty property)
	{
		return getPropertyValue(property.getName());
	}
	
	public String getPropertyValueString(String property)
	{
		return (String)getPropertyValue(property);
	}
	
	public void setPropertyValue(IModelProperty property, Object value)
	{
		setPropertyValue(property.getName(), value);
	}
	
	public void setPropertyValue(String property, Object value)
	{
		Object oldValue = getPropertyValue(property);
		
		values.put(property, value);
		
		firePropertyEvent(oldValue, value);
	}
	
	private void firePropertyEvent(Object oldValue, Object newValue)
	{
		if (listeners.size() == 0)
			return;
		
		// TODO:
		
	}

	public final IModelClass getModelClass()
	{
		if (modelClass == null)
			modelClass = new ModelClass(getClass());
		
		return modelClass;
	}

	public void addPropertyChangeListener(IPropertyListener listener)
	{
		listeners.add(listener);
	}
	
	public void removePropertyChangeListener(IPropertyListener listener)
	{
		listeners.remove(listener);
	}
	
}
